<?php
/**
 *
 *
 *
 */
class Yz_Form_Element_Input
    extends Yz_Form_Element_Abstract
    implements Yz_Form_Element_Interface
{

    protected $name;
    protected $value = "";


    /**
     *
     */
    public function render(Yz_Template_Interface $template)
    {
        $class = $this->getCSSClass();
        $o = <<<html
            <input type="text" name="{$this->name}" value="{$this->value}" class="{$class}" />\n
html;
        return $o;
    }

    /**
     *  @return this
     *  @throw Yz_Form_Element_Exception
     */
    public function parse(Yz_Hash $values)
    {
        debug("Element '$this->name'");
        if ($values->offsetExists($this->name)) {
            $value = Yz_String::valueOf($values[$this->name]);
            if (null === $value) {
                $ex = new Yz_Form_Element_Exception();
                throw $ex->withArgs(array('name' => $this->name));
            }
        } else {
            $value = "";
        }

        if ("" === $value && ! $this->allowEmpty()) {
            $ex = new Yz_Form_Element_Exception();
            throw $ex->withArgs(array('name' => $this->name));
        }
        $this->value = $value;
        return $this;
    }

}
